<template>
	<view class="icons" @click.stop="likeClick">
		<uni-icons size="20" color="#f07373" :type="like ? 'heart-filled' : 'heart'"></uni-icons>
	</view>
</template>

<script>
	export default {
		props: {
			item: {
				type: Object,
				default: () => {}
			},
			types: {
				type: String,
				defual: ''
			}
		},
		data() {
			return {
				like: false
			}
		},
		watch: {
			item (newVal) {
				this.like = this.item.is_like
			}
		},
		created () {
			this.like = this.item.is_like
		},
		methods: {
			likeClick () {
				this.like = !this.like
				this.updateLikes()
			},
			updateLikes () {
				uni.showLoading()
				this.$api.update_likes({
					userId: '5f354465ad57420001c700e6',
					articleId: this.item._id
				}).then((res) => {
					uni.hideLoading()
					uni.showToast({
						title: this.like ? '收藏成功' : '取消收藏成功',
						icon: 'none'
					})
					uni.$emit('changAticleLike', this.types)
				}).catch(() => {})
			}
		}
	}
</script>

<style lang="scss">
	.icons {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 20px;
		height: 20px;
		position: absolute;
		right: 0;
		top: 0;
	}
</style>
